package com.lcl.jarvis.admin.modules.person.service.impl;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcl.jarvis.admin.modules.person.dto.request.TbEpidemiologicalInfoReqDTO;
import com.lcl.jarvis.admin.modules.person.dto.response.TbEpidemiologicalInfoRespDTO;
import com.lcl.jarvis.admin.modules.person.entity.TbEpidemiologicalInfo;
import com.lcl.jarvis.admin.modules.person.entity.TbPersonImages;
import com.lcl.jarvis.admin.modules.person.service.ITbEpidemiologicalInfoService;
import com.lcl.jarvis.admin.modules.person.service.ITbPersonImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lcl.jarvis.common.model.PageResult;
import com.lcl.jarvis.admin.modules.person.dto.request.TbPersonInfoDReqDTO;
import com.lcl.jarvis.admin.modules.person.dto.response.TbPersonInfoDRespDTO;
import com.lcl.jarvis.admin.modules.person.dto.request.TbPersonInfoDPageReqDTO;

import com.lcl.jarvis.admin.modules.person.mapper.TbPersonInfoDMapper;
import com.lcl.jarvis.admin.modules.person.entity.TbPersonInfoD;
import com.lcl.jarvis.admin.modules.person.service.ITbPersonInfoDService;
import lombok.RequiredArgsConstructor;

/**
 * 数据筛查管理Service业务层处理
 *
 * @author licl
 * @date 2024-06-21 16:30:49
 */
@Service
@RequiredArgsConstructor
public class TbPersonInfoDServiceImpl extends ServiceImpl<TbPersonInfoDMapper, TbPersonInfoD> implements ITbPersonInfoDService {
    @Autowired
    private ITbEpidemiologicalInfoService tbEpidemiologicalInfoService;

    @Autowired
    private ITbPersonImagesService tbPersonImagesService;

    /**
     * 新增数据筛查管理
     *
     * @param body 数据筛查管理
     * @return 结果
     */
    @Override
    public Integer add(TbPersonInfoDReqDTO body) {
        TbPersonInfoD entity = BeanUtil.copyProperties(body, TbPersonInfoD.class);
        baseMapper.insert(entity);
        // 新增相关疾病信息
        TbEpidemiologicalInfoReqDTO epidemiologicalInfo = body.getEpidemiologicalInfoDTO();
        epidemiologicalInfo.setQueryId(entity.getQueryId());
        epidemiologicalInfo.setPersonId(entity.getId().toString());
        tbEpidemiologicalInfoService.add(epidemiologicalInfo);
        return 1;
    }

    /**
     * 批量删除数据筛查管理
     *
     * @param ids 需要删除的数据筛查管理主键
     * @return 结果
     */
    @Override
    public Integer remove(List<String> ids) {
        baseMapper.deleteByIds(ids);
        // 删除相关疾病信息
        tbEpidemiologicalInfoService.remove(Wrappers.<TbEpidemiologicalInfo>lambdaQuery().in(TbEpidemiologicalInfo::getPersonId, ids));
        // 删除图片
        tbPersonImagesService.remove(Wrappers.<TbPersonImages>lambdaQuery().in(TbPersonImages::getPersonId, ids));
        return 1;
    }

    /**
     * 修改数据筛查管理
     *
     * @param body 数据筛查管理
     * @return 结果
     */
    @Override
    public Integer update(TbPersonInfoDReqDTO body) {
        TbPersonInfoD entity = BeanUtil.copyProperties(body, TbPersonInfoD.class);
        baseMapper.updateById(entity);
        // 新增相关疾病信息
        TbEpidemiologicalInfo epidemiologicalInfo =  BeanUtil.copyProperties(body.getEpidemiologicalInfoDTO(), TbEpidemiologicalInfo.class);
        tbEpidemiologicalInfoService.updateById(epidemiologicalInfo);
        return 1;
    }


    /**
     * 查询数据筛查管理
     *
     * @param id 数据筛查管理主键
     * @return 数据筛查管理
     */
    @Override
    public TbPersonInfoDRespDTO detail(String id) {
        TbPersonInfoD entity = baseMapper.selectById(id);
        TbPersonInfoDRespDTO dto = BeanUtil.copyProperties(entity, TbPersonInfoDRespDTO.class);
        TbEpidemiologicalInfo epidemiologicalInfo = tbEpidemiologicalInfoService.selectByPersonId(entity.getId());
        dto.setEpidemiologicalInfoDTO(BeanUtil.copyProperties(epidemiologicalInfo, TbEpidemiologicalInfoRespDTO.class));
        return dto;
    }

    /**
     * 查询数据筛查管理列表
     *
     * @param req
     * @return 数据筛查管理
     */
    @Override
    public PageResult<TbPersonInfoDRespDTO> page(TbPersonInfoDPageReqDTO req) {
        LambdaQueryWrapper<TbPersonInfoD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(req.getPersonName()), TbPersonInfoD::getPersonName, req.getPersonName());
        queryWrapper.eq(StrUtil.isNotBlank(req.getQueryUnit()), TbPersonInfoD::getQueryUnit, req.getQueryUnit());
        Page<TbPersonInfoD> page = baseMapper.selectPage(req.buildPage(), queryWrapper);
        return PageResult.of(page, TbPersonInfoDRespDTO.class);
    }

}
